﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using CE.Provider;
using CE.Global;

public partial class Provider_CarInsurance_ProviderCarCapacityExcessClaim : System.Web.UI.Page
{
    string PageName = "CXCI";
    string SubPageName = "CXCIEXSS";
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Session[CE.Global.General.UserId] == null || Session[CE.Global.General.ProviderCompanyId] == null)
        {
            Response.Redirect("~/Provider/ProviderSecureLogin.aspx");
        }
        CheckMainPageAccess();
        CheckSubPageAccess();

        if (!Page.IsPostBack)
        {
            ClearFields();
            usrEffectivityDate.StartYear = CE.Global.General.GetUTCDateTime().Year;
            usrEffectivityDate.EndYear = CE.Global.General.GetUTCDateTime().Year + 2; 

            CarClaimExpInfoList _claiminfolist = new CarClaimExpInfoList();
            CarClaimExpInfoDB _claiminfodb = new CarClaimExpInfoDB();
            _claiminfolist = _claiminfodb.GetProviderCarClaimExpInfoList(Convert.ToInt64(Session[General.ProviderCompanyId].ToString()));

            if (_claiminfolist != null)
            {
                for (int i = 0; i < _claiminfolist.Count; i++)
                {
                    ddlClaimValue.Items.Add(new ListItem(_claiminfolist[i].Description, _claiminfolist[i].ProviderCarClaimExpId.ToString()));
                }
            }

            ddlClaimValue.Items.Insert(0, new ListItem("Select", "-1"));
            ddlClaimValue.Text = "-1";

            if (Page.Request["FromCC"] != null &&
                Page.Request["ToCC"] != null &&
                Page.Request["ClaimValue"] != null)
            {
                int _fromcc = 0;
                int _tocc = 0;
                int _claimvalue = 0;
                if (int.TryParse(Page.Request["FromCC"].ToString(), out _fromcc) &&
                    int.TryParse(Page.Request["ToCC"].ToString(), out _tocc) &&
                    int.TryParse(Page.Request["ClaimValue"].ToString(), out _claimvalue))
                {
                    ShowCapacityExcessClaim(Convert.ToInt64(Session[General.ProviderCompanyId].ToString()), _claimvalue, _fromcc, _tocc);
                }
            }
            else if (Page.Request["CarClaimExcessId"] != null)
            {
                ddlClaimValue.Text = Page.Request["CarClaimExcessId"].ToString();
            }

            Page.Form.DefaultButton = ibtSubmit.UniqueID;
        }
    }

    protected void ibtSubmit_Click(object sender, ImageClickEventArgs e)
    {
        if (Page.IsValid)
        {
            string _authcode = CE.Security.MySecurity.Encrypt(txtAuthCode.Text, true);
            if (CheckAuthCode(_authcode, 100))
            {

                CarCapacityExcessClaim _claimexcess = new CarCapacityExcessClaim();
                CarCapacityExcessClaimDB _claimexcessdb = new CarCapacityExcessClaimDB();

                if (lblProviderCarCapacityExcessClaimId.Text != "0")
                {
                    _claimexcess.ProviderCarCapacityExcessClaimId = Convert.ToInt64(lblProviderCarCapacityExcessClaimId.Text);
                    if (CheckCapacityExcessClaimDuplicate(_claimexcess.ProviderCarCapacityExcessClaimId, Convert.ToInt64(Session[General.ProviderCompanyId].ToString()), Convert.ToInt32(ddlClaimValue.Text), Convert.ToInt32(txtFromCCValue.Text), Convert.ToInt32(txtToCCValue.Text)))
                    {
                        lblMessage.Text = "This setting is alredy exist.";
                        return;
                    }
                }
                else
                {
                    _claimexcess.ProviderCarCapacityExcessClaimId = CheckCapacityExcessClaim(Convert.ToInt64(Session[General.ProviderCompanyId].ToString()), Convert.ToInt32(ddlClaimValue.Text), Convert.ToInt32(txtFromCCValue.Text), Convert.ToInt32(txtToCCValue.Text));
                }

                int _trantype = 1;
                if (_claimexcess.ProviderCarCapacityExcessClaimId > 0)
                {
                    _trantype = 2;
                }


                _claimexcess.RefCEUserId = Convert.ToInt64(Session[General.UserId].ToString());
                _claimexcess.ProviderCompanyId = Convert.ToInt64(Session[General.ProviderCompanyId].ToString());

                _claimexcess.ProviderCarClaimExpId = Convert.ToInt32(ddlClaimValue.Text);
                _claimexcess.FromCC = Convert.ToInt32(txtFromCCValue.Text);
                _claimexcess.ToCC = Convert.ToInt32(txtToCCValue.Text);
                _claimexcess.ExcessAmount = Convert.ToSingle(txtExcessAmount.Text);
                _claimexcess.Status = chkStatus.Checked;

                if (rdoEffectivity.SelectedValue == "Yes" && usrEffectivityDate.GetDate() > CE.Global.General.GetUTCDateTime())
                {
                    _claimexcess.FInfo1 = float.Parse(txtNewRate.Text);
                    _claimexcess.DInfo1 = usrEffectivityDate.GetDate();

                }
                else
                {
                    _claimexcess.FInfo1 = float.Parse(txtNewRate.Text);
                    _claimexcess.DInfo1 = CE.Global.General.GetUTCDateTime();

                }

                _claimexcess.ProviderCarCapacityExcessClaimId = _claimexcessdb.Insert(_claimexcess);
                if (_claimexcess.ProviderCarCapacityExcessClaimId > 0)
                {
                    InsertProviderAuthTransaction(_claimexcess.ProviderCarCapacityExcessClaimId, "CICarExcess", _trantype, 100, _authcode, _claimexcess.ProviderCompanyId, _claimexcess.RefCEUserId);
                    ProviderUpdateSave(_claimexcess.ProviderCarCapacityExcessClaimId);
                    lblMessage.Text = "Update successfully!";
                    Response.Redirect("ViewProviderCarCapacityExcessClaim.aspx?CarClaimExcessId=" + ddlClaimValue.Text);
                }
                else
                {
                    lblMessage.Text = "Updating failed.";
                }
            }
            else
            {
                lblMessage.Text = "Wrong auth code!";
            }
        }
    }

    private void ProviderUpdateSave(Int64 ProviderRefId)
    {
        ProviderUpdates _providerupdates = new ProviderUpdates();
        ProviderUpdatesDB _providerupdatesDB = new ProviderUpdatesDB();

        _providerupdates.UpdateId = 0;
        _providerupdates.CXTableId = GetCXTableId();
        _providerupdates.ProviderRefId = ProviderRefId;

        if (rdoEffectivity.SelectedValue == "Yes" && usrEffectivityDate.GetDate() > CE.Global.General.GetUTCDateTime())
        {
            //Scheduled
            _providerupdates.UpdateType = 1;
        }
        else
        {
            //Immediate
            _providerupdates.UpdateType = 0;
        }

        _providerupdates.ProviderCompanyId = Convert.ToInt64(Session[CE.Global.General.ProviderCompanyId]);
        _providerupdates.RefCEUserID = Convert.ToInt64(Session[General.UserId].ToString());

        _providerupdatesDB.Insert(_providerupdates);

    }

    private Int32 GetCXTableId()
    {
        Int32 CXID = 0;

        ProviderTableListings _tablelist = new ProviderTableListings();
        ProviderTableListingsDB _tablelistdb = new ProviderTableListingsDB();

        int ProviderType = 100; //Car Insurance
        string CXTable = "tblProviderCarCapacityExcessByClaim";

        _tablelist = _tablelistdb.GetProviderTableListingsSingleItem(ProviderType, Convert.ToInt64(Session[CE.Global.General.ProviderCompanyId]), CXTable);

        if (_tablelist != null)
            CXID = _tablelist.CXTableId;

        return CXID;

    }

    private void ClearFields()
    {
        lblProviderCarCapacityExcessClaimId.Text = "0";
        ddlClaimValue.Text = "-1";
        txtFromCCValue.Text = "";
        txtToCCValue.Text = "";
        txtExcessAmount.Text = "";
        chkStatus.Checked = false;
    }

    protected void ibtBack_Click(object sender, ImageClickEventArgs e)
    {
        Response.Redirect("ViewProviderCarCapacityExcessClaim.aspx?CarClaimExcessId=" + ddlClaimValue.Text);
    }

    protected void btnDelete_Click(object sender, EventArgs e)
    {
        rfvAuthCode.Validate();
        if (lblProviderCarCapacityExcessClaimId.Text != "0" && txtAuthCode.Text.Trim().Length > 0)
        {
            Int64 _carclaimid = Convert.ToInt64(lblProviderCarCapacityExcessClaimId.Text);
            Int64 _providercompanyid = Convert.ToInt64(Session[General.ProviderCompanyId].ToString());
            string _authcode = CE.Security.MySecurity.Encrypt(txtAuthCode.Text, true);
            Int64 _refceuserid = Convert.ToInt64(Session[General.UserId].ToString());


            if (CheckAuthCode(_authcode, 100))
            {
                if (_carclaimid != 0)
                {
                    CarCapacityExcessClaimDB _carclaimdb = new CarCapacityExcessClaimDB();
                    if (_carclaimdb.Delete(_carclaimid, _providercompanyid))
                    {
                        InsertProviderAuthTransaction(_carclaimid, "CICarExcess", 3, 100, _authcode, _providercompanyid, _refceuserid);
                        Response.Redirect("ViewProviderCarCapacityExcessClaim.aspx");
                    }
                    else
                    {
                        lblMessage.Text = "Deleting failed.";
                    }
                }
            }
            else
            {
                lblMessage.Text = "Wrong auth code!";
            }
        }
    }

    private void ShowCapacityExcessClaim(Int64 ProviderCompanyId, Int64 ProviderClaimInfoId, int FromCC, int ToCC)
    {
        CarCapacityExcessClaim _carclaim = new CarCapacityExcessClaim();
        CarCapacityExcessClaimDB _carclaimdb = new CarCapacityExcessClaimDB();

        _carclaim = _carclaimdb.GetProviderCarCapacityExcessClaim(ProviderCompanyId, ProviderClaimInfoId, FromCC, ToCC);

        if (_carclaim != null)
        {
            lblProviderCarCapacityExcessClaimId.Text = _carclaim.ProviderCarCapacityExcessClaimId.ToString();
            ddlClaimValue.Text = _carclaim.ProviderCarClaimExpId.ToString();
            txtFromCCValue.Text = _carclaim.FromCC.ToString();
            txtToCCValue.Text = _carclaim.ToCC.ToString();
            txtExcessAmount.Text = _carclaim.ExcessAmount.ToString("#.00");
            chkStatus.Checked = _carclaim.Status;

            if (_carclaim.DInfo1 > CE.Global.General.GetUTCDateTime())
            {
                divNewRate.Visible = true;
                txtNewRate.Text = _carclaim.FInfo1.ToString();
                rdoEffectivity.SelectedValue = "Yes";
            }
            else
            {
                divNewRate.Visible = false;
                rdoEffectivity.SelectedValue = "No";
            }

            usrEffectivityDate.Day = _carclaim.DInfo1.Day;
            usrEffectivityDate.Month = _carclaim.DInfo1.Month;
            usrEffectivityDate.Year = _carclaim.DInfo1.Year;
        }
    }
    
    private Int64 CheckCapacityExcessClaim(Int64 ProviderCompanyId, Int64 ProviderClaimInfoId, int FromCC, int ToCC)
    {
        CarCapacityExcessClaim _carclaim = new CarCapacityExcessClaim();
        CarCapacityExcessClaimDB _carclaimdb = new CarCapacityExcessClaimDB();
        Int64 _retValue = 0;

        _carclaim = _carclaimdb.GetProviderCarCapacityExcessClaim(ProviderCompanyId, ProviderClaimInfoId, FromCC, ToCC);

        if (_carclaim != null)
        {
            _retValue = _carclaim.ProviderCarCapacityExcessClaimId;
        }

        return _retValue;
    }

    private bool CheckCapacityExcessClaimDuplicate(Int64 CurrentProviderCarCapacityExcessClaimId, Int64 ProviderCompanyId, Int64 ProviderClaimExpId, int FromCC, int ToCC)
    {
        bool _retValue = false;
        CarCapacityExcessClaimDB _carclaimdb = new CarCapacityExcessClaimDB();

        _retValue = _carclaimdb.CheckProviderCarCapacityExcessClaimDuplicate(CurrentProviderCarCapacityExcessClaimId, ProviderCompanyId, ProviderClaimExpId, FromCC, ToCC);

        return _retValue;
    }

    protected void btnNew_Click(object sender, EventArgs e)
    {
        ClearFields();
    }

    public void EffectiveWhen(object sender, EventArgs e)
    {
        if (rdoEffectivity.SelectedValue == "Yes")
        {
            divNewRate.Visible = true;
        }
        else
        {
            divNewRate.Visible = false;
        }

        usrEffectivityDate.Day = CE.Global.General.GetUTCDateTime().Day;
        usrEffectivityDate.Month = CE.Global.General.GetUTCDateTime().Month;
        usrEffectivityDate.Year = CE.Global.General.GetUTCDateTime().Year;
    }

    protected bool CheckAuthCode(string strAuthCode, int intAuthCodeType)
    {
        bool _retValue = false;
        ProviderAuthCode _authcode = new ProviderAuthCode();
        ProviderAuthCodeDB _authcodedb = new ProviderAuthCodeDB();
        _authcode = _authcodedb.GetProviderAuthCode(Convert.ToInt64(Session[General.ProviderCompanyId].ToString()), intAuthCodeType, strAuthCode);
        if (_authcode != null)
        {
            _retValue = true;
        }

        return _retValue;
    }

    protected Int64 InsertProviderAuthTransaction(Int64 TransactionId, string TransactionCode, int TransactionType,
        int AuthCodeType, string AuthCode, Int64 ProviderCompanyId, Int64 RefCEUserId)
    {
        ProviderAuthTransaction _authtrans = new ProviderAuthTransaction();
        ProviderAuthTransactionDB _authtransdb = new ProviderAuthTransactionDB();

        _authtrans.TransactionId = TransactionId;
        _authtrans.TransactionCode = TransactionCode;
        _authtrans.TransactionType = TransactionType;
        _authtrans.AuthCode = AuthCode;
        _authtrans.AuthCodeType = AuthCodeType;
        _authtrans.ProviderCompanyId = ProviderCompanyId;
        _authtrans.RefCEUserID = RefCEUserId;

        _authtrans.AuthTransId = _authtransdb.Insert(_authtrans);
        return _authtrans.AuthTransId;
    }

    private void CheckMainPageAccess()
    {
        Int64 PageAccess = General.GetProviderMainPageAccess(Convert.ToInt64(Session[General.ProviderCompanyId].ToString()),
                                                        Convert.ToInt64(Session[General.UserId].ToString()), PageName);
        if (PageAccess == 0)
        {
            //Response.Redirect("~/Provider/UserMessage.aspx?errorcode=2000");
            Response.Redirect("~/Provider/UserMessage.aspx?mt=UM&ec=2000");
        }
    }

    private void CheckSubPageAccess()
    {
        Int64 PageAccess = General.GetProviderSubPageAccess(Convert.ToInt64(Session[General.ProviderCompanyId].ToString()),
                                                        Convert.ToInt64(Session[General.UserId].ToString()), SubPageName);
        if (PageAccess == 0)
        {
            //Response.Redirect("~/Provider/UserMessage.aspx?errorcode=2000");
            Response.Redirect("~/Provider/UserMessage.aspx?mt=UM&ec=2000");
        }
        else if (PageAccess == 100)
        {
            ibtSubmit.Enabled = false;
            btnNew.Enabled = false;
            btnDelete.Enabled = false;
        }
        else if (PageAccess == 200)
        {
            ibtSubmit.Enabled = true;
            btnNew.Enabled = true;
            btnDelete.Enabled = false;
        }
        else if (PageAccess == 300)
        {
            ibtSubmit.Enabled = true;
            btnNew.Enabled = true;
            btnDelete.Enabled = true;
        }
    }

}
